/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Map;

import Actor.TileObject;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.IOException;
import javax.vecmath.Point2d;

/**
 *
 * @author Eric-Standard
 */
public class Map {

    public static final int MAP_WIDTH = 20;
    public static final int TILE_WIDTH = 32;
    public static final int TILE_HEIGHT = TILE_WIDTH;
    public Tile[][] arr;
    public TileObject[][] mapObjects;
    private int height;
    public static Tile[] DEFAULT_TILES;

    static {
        Tile grass;
        Tile water;
        Tile rock;
        Tile wall;
        water = new ColorTile(Color.BLUE);
        try {
            grass = new StaticTile("images/grass.png");
            water = new StaticTile("images/water.png");
            rock = new WallTile("images/rock.png");
            wall = new WallTile("images/wall.png");
        } catch (IOException ex) {
            System.err.println(ex);
            grass = null;
            water = null;
            rock = null;
            wall = null;
        }
        DEFAULT_TILES = new Tile[]{null, grass, water, rock, wall};
    }

    public Map() {
    }

    public boolean isValid(int x, int y) {
        if (x < 0 || y < 0) {
            return false;
        } else if (x >= MAP_WIDTH || y >= arr.length) {
            return false;
        }
        return true;
    }

    public void setHeight(int height) {
        this.height = height;
        arr = new Tile[height][MAP_WIDTH];
    }

    public void drawTile(Graphics g, int x, int y, int sx, int sy) {
        if (isValid(x, y)) {
            arr[y][x].draw(g, sx, sy);
        }
    }

    public TileObject[][] getTileObjects() {
        return mapObjects;
    }

    public TileObject getTileObject(int x, int y) {
        if (isValid(x, y)) {
            return mapObjects[y][x];
        }
        return null;
    }

    public TileObject getTileObject(Point p) {
        return getTileObject(p.x, p.y);
    }

    public void generateMapObjects() {
        mapObjects = new TileObject[height][MAP_WIDTH];
        for (int i = 0; i < arr.length; i++) {
            Tile[] tiles = arr[i];
            for (int j = 0; j < tiles.length; j++) {
                Tile tile = tiles[j];
                if (tile.hasObject) {
                    TileObject tileObject = tile.getTileObject();
                    if (tileObject != null) {
                        tileObject.setPos(new Point2d(j * TILE_WIDTH, i * TILE_HEIGHT));
                        mapObjects[i][j] = tileObject;
                    }
                } else {
                    mapObjects[i][j] = null;
                }
            }
        }
    }
}
